package cn.ekuma.epos.erp.link;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import cn.ekuma.epos.order.model.OrderInfoTableModel;

import com.openbravo.bean.erp.viewbean.FindOrdersInfo;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.data.user.EditorCreator;
import com.openbravo.pos.panels.QuickTimerFilter;
import com.openbravo.pos.panels.event.TimeCycleChangedListener;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BoxLayout;
import org.jdesktop.swingx.JXTable;

public abstract class AbstractOrderLinkPanel extends JPanel implements TimeCycleChangedListener,EditorCreator{
	protected OrderInfoTableModel tableModel;
	private QuickTimerFilter timerFilter;
	protected JXTable jXTable1;
	JPanel panel_1;
	protected String ID;
	
	
	public AbstractOrderLinkPanel() {
		initComponents();
		timerFilter.setDefineSelected(QuickTimerFilter.TIMERCYCLETYPE_Week);
		timerFilter.addTimeCycleChangedListener(this);
		tableModel=new OrderInfoTableModel(null);
		jXTable1.setModel(tableModel);
		initToolBar(panel_1);
	}
	
	public  void initToolBar(JPanel toolBar) {
	}

	private void initComponents() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		timerFilter=new QuickTimerFilter();
		panel.add(timerFilter, BorderLayout.WEST);
		
		panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
		JScrollPane scrollPane = new JScrollPane();


		jXTable1 = new JXTable();
		jXTable1.setColumnControlVisible(true);
	    jXTable1.setAutoCreateRowSorter(true);
	    jXTable1.setAutoResizeMode(JXTable.AUTO_RESIZE_OFF);
	    jXTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	    jXTable1.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e) {
				int selectVCol=jXTable1.getSelectedRow();
				if(e.getClickCount()==2&&selectVCol>-1){
					FindOrdersInfo orderInfo=(FindOrdersInfo)tableModel.getRow(jXTable1.convertRowIndexToModel(selectVCol));
					fireOrderInfoChangedListener(orderInfo);
				}
			}
	    });
	    scrollPane.setViewportView(jXTable1);
	    add(scrollPane, BorderLayout.CENTER);
	}
	
	public abstract void fireOrderInfoChangedListener(FindOrdersInfo orderInfo);

	protected boolean isIDChanged(String inOrderID) {
	    if(inOrderID==null){
	        if(ID!=null)
	            return true;
	    }else if(ID==null)
	            return true;
	      else if(!ID.equalsIgnoreCase(inOrderID))
	            return true;
	    return false;
	}

    public Object[] createValue() throws BasicException {
       Object[] afilter = new Object[18];
       
       // Order ID
       afilter[0] = QBFCompareEnum.COMP_NONE;
       afilter[1] = null;
       
       //Order DefineType
       afilter[2] = QBFCompareEnum.COMP_NONE;
       afilter[3] = null;
       
       // Sale and refund checkbox
           afilter[4] = QBFCompareEnum.COMP_NONE;
           afilter[5] = 2;

       // Date range
          afilter[6] = (timerFilter.getStartDate()== null)? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_GREATEROREQUALS;
 	      afilter[7] = timerFilter.getStartDate();
 	      afilter[8] = ( timerFilter.getEndDate() == null) ? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_LESS;
 	      afilter[9] = timerFilter.getEndDate();

       //User
           afilter[10] = QBFCompareEnum.COMP_NONE;
           afilter[11] = null;
       
       //Customer
           afilter[12] = QBFCompareEnum.COMP_NONE;
           afilter[13] = null;
       
       //Location
           afilter[14] = QBFCompareEnum.COMP_NONE;
           afilter[15] = null;
       
       // Receipt money
       afilter[16] = QBFCompareEnum.COMP_NONE ;
       afilter[17] =null;

       return afilter;

   }
}
